import React, { Component } from 'react'
import PropTypes from 'prop-types'
import commonTypes from '../../utils/commonTypes'
import withDataList from '../../HOC/withDataList'

/**
 * 一组单选框
 */
class RadioBox extends Component {

    static propTypes = {
        value: PropTypes.string,
        optionItem: commonTypes.optionItem.isRequired,
        name: PropTypes.string.isRequired,
        onChange: PropTypes.func
    }

    onChange = (e) => {
        const { onChange } = this.props;
        onChange && onChange(e.target.value)
    }

    render() {
        const { value, name, optionItem } = this.props
        return (
            <label>
                <input
                    type="radio"
                    name={name}
                    value={optionItem.value}
                    checked={value === optionItem.value}
                    onChange={this.onChange}
                />
                <span>{optionItem.label}</span>
            </label>
        )
    }
}

export default withDataList(RadioBox)
